package com.itheima.wechat;

import com.google.code.kaptcha.impl.DefaultKaptcha;

import com.itheima.entity.Result;
import com.itheima.utils.JedisUtil;
import com.itheima.utils.SmsUtil;
import com.itheima.utils.ValidateCodeUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.Map;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {
    @Autowired
    DefaultKaptcha defaultKaptcha;

    @RequestMapping("/code/{deviceId}")
    public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
        Assert.notNull(deviceId, "机器码不能为空");
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = defaultKaptcha.createText();
        //生成图片验证码
        BufferedImage image = defaultKaptcha.createImage(text);
        //生成的验证码写入redis
        jedisUtil.setex(deviceId, 60 * 5, text);
        //获取输出流
        ServletOutputStream out = response.getOutputStream();
        //将图片写回浏览器
        ImageIO.write(image, "JPEG", out);
    }

    @Autowired
    JedisUtil jedisUtil;

    @RequestMapping("/send4Order")
    public Result send4Order(String telephone,String imgCode,String deviceId){
        if("".equals(imgCode)){
            return Result.error("请先输入图形验证码");
        }
        String jcode = jedisUtil.get(deviceId);
        if(!imgCode.equals(jcode)){
            return Result.error("图形验证码有误，请重新输入");
        }
        //生成验证码
        try {
            Integer code = ValidateCodeUtils.generateValidateCode(6);
            //发送短信
//            SendSmsUtil.sendSms(telephone, String.valueOf(code));
            //将验证码存入redis
            System.out.println(code);
            jedisUtil.add(telephone+"001",60*5*100,String.valueOf(code));
            return Result.success("");
        } catch (Exception e) {
            return Result.error("验证码发送失败，请检查手机号输入是否正确");
        }
    }

    @RequestMapping("/send4Login")
    public Result send4Login(@RequestBody Map<String, String> map) {
        String telephone = map.get("telephone");
        String deviceId = map.get("deviceId");
        String imgCode = map.get("code");

        String redisImgCode = jedisUtil.get(deviceId);
        if (null == redisImgCode || !redisImgCode.equals(imgCode)) {
            return Result.error("图形验证码输入错误");
        }
        //生成验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        //调用短信接口发送验证
//        SmsUtil.sendSmsCode(telephone,String.valueOf(code));
        System.out.println("code:" + code);

        jedisUtil.setex(telephone+"002", 60 * 60 * 24, String.valueOf(code));

        return Result.success("");
    }
}